/**
 * 茶叶档案管理初始化
 */
var TeaFile = {
    id: "TeaFileTable",	//表格id
    seItems: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TeaFile.initColumn = function () {
    return [
        // {field: 'selectItem', radio: true},
        {field: 'selectItem', checkbox: true},
        {title: 'id', field: 'id', visible: true, align: 'center', valign: 'middle'},
        {title: '茶叶名称', field: 'name', visible: true, align: 'center', valign: 'middle'},
        {title: '茶叶编码', field: 'code', visible: true, align: 'center', valign: 'middle'},
        {title: '种类', field: 'type', visible: true, align: 'center', valign: 'middle'},
        {title: '产地', field: 'address', visible: true, align: 'center', valign: 'middle'},
        // {title: '价格', field: 'price', visible: true, align: 'center', valign: 'middle'},
        {title: '保质天数', field: 'expiryDay', visible: true, align: 'center', valign: 'middle'},
        // {title: '冲泡方式', field: 'brewsId', visible: true, align: 'center', valign: 'middle'},
        {title: '工艺', field: 'technology', visible: true, align: 'center', valign: 'middle'},
        // {title: '加工方法', field: 'processMethod', visible: true, align: 'center', valign: 'middle'},
        {title: '存储方法', field: 'storageMethod', visible: true, align: 'center', valign: 'middle'},
        {title: '包装净重(g)', field: 'weight', visible: true, align: 'center', valign: 'middle'},
        {title: '包装种类', field: 'packingType', visible: true, align: 'center', valign: 'middle'},
        {title: '包装方式', field: 'packingMethod', visible: true, align: 'center', valign: 'middle'},
        {title: '生产日期', field: 'productionDate', visible: true, align: 'center', valign: 'middle'},
        {title: '包装时间', field: 'packingDate', visible: true, align: 'center', valign: 'middle'},
        {
            field: 'operate',
            title: '查看二维码',
            align: 'center',
            width: 100,
            events: operateEvents,
            formatter: operateFormatter
        }
    ];
};

/**
 * 检查是否选中
 */
TeaFile.check = function (type) {
    var selecteds = $('#' + this.id).bootstrapTable('getSelections');
    if (selecteds.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else if (selecteds.length > 1 && type == "edit") {
        Feng.info("只能同时编辑一条记录！");
        return false;
    } else {
        TeaFile.seItems = selecteds;
        return true;
    }
};

/**
 * 点击添加茶叶档案
 */
TeaFile.openAddTeaFile = function () {
    var index = layer.open({
        type: 2,
        title: '添加茶叶档案',
        area: ['1000px', '620px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/teaFile/teaFile_add'
    });
    this.layerIndex = index;
    layer.full(index);
};

/**
 * 打开编辑茶叶基本信息
 */
TeaFile.openTeaFileDetail = function () {
    if (this.check("edit")) {
        var index = layer.open({
            type: 2,
            title: '修改茶叶档案',
            area: ['1000px', '620px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/teaFile/teaFile_update/' + TeaFile.seItems[0].id
        });
        this.layerIndex = index;
        layer.full(index);
    }
};

/**
 * 打开编辑茶叶更多详情
 */
TeaFile.editDetail = function () {
    if (this.check("edit")) {
        var index = layer.open({
            type: 2,
            title: '修改茶叶更多详情',
            area: ['1000px', '620px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/teaFile/teaFile_edit/' + TeaFile.seItems[0].id
        });
        this.layerIndex = index;
        layer.full(index);
    }
};

/**
 * 打开二维码页面
 * @param id 茶叶id
 */
TeaFile.showQR = function (id) {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    TeaFile.seItem = selected[0];

    var index = layer.open({
        type: 2,
        title: "茶叶id：" + id,
        area: ['350px', '370px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/teaFile/QR_show/' + id
    });
    this.layerIndex = index;
};

/**
 * 删除茶叶档案
 */
TeaFile.delete = function () {
    if (this.check("del")) {
        // 遍历删除选中记录
        var operation = function () {
            for (idx in TeaFile.seItems) {
                var ajax = new $ax(Feng.ctxPath + "/teaFile/delete", function (data) {
                    Feng.success("删除成功!您还可以在回收站中找回该条信息");
                    TeaFile.table.refresh();
                }, function (data) {
                    Feng.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.set("teaFileId", TeaFile.seItems[idx].id);
                ajax.set("teaName", TeaFile.seItems[idx].name);
                ajax.start();
            }
        };
        Feng.confirm("是否删除选中的茶叶信息?", operation);
    }
};

/**
 * 查询茶叶档案列表
 */
TeaFile.search = function () {
    var queryData = {};
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['condition'] = $("#condition").val();
    TeaFile.table.refresh({query: queryData});
};

/**
 * 重置搜索条件
 */
TeaFile.resetSearch = function () {
    $("#condition").val("");
    $("#beginTime").val("");
    $("#endTime").val("");

    TeaFile.search();
}

/**
 * 下载二维码到本地
 */
TeaFile.downLoadQR = function () {
    if (this.check()) {
        //拼接选中的茶叶id（用小横杠来拼接）
        for (var i = 1, idValue = TeaFile.seItems[0].id; i < TeaFile.seItems.length; i++) {
            idValue = idValue + "-" + TeaFile.seItems[i].id;
            if (i != TeaFile.seItems.length - 1) {
                idValue = idValue + "-";
            }
        }

        window.location.href = Feng.ctxPath + "/teaFile/download?ids=" + idValue;
    }
};

/**
 * 导出表格数据
 */
TeaFile.exportTable = function () {
    $('#user-table').tableExport({
        type: 'excel',
        escape: 'false',
        fileName: '茶叶数据'
    });
}


$(function () {
    var defaultColunms = TeaFile.initColumn();
    var table = new BSTable(TeaFile.id, "/teaFile/list", defaultColunms);
    TeaFile.table = table.init();
});

/**
 * 点击“查看二维码”
 * @type {{[click .RoleOfdelete]: Window.operateEvents.click .RoleOfdelete}}
 */
window.operateEvents = {
    'click .RoleOfdelete': function (e, value, row, index) {
        TeaFile.showQR(row.id);
    },
};
function operateFormatter(value, row, index) {
    return [
        '<button type="button" class="RoleOfdelete btn btn-primary  btn-sm" style="margin-right:15px;">查看</button>',
    ].join('');
}